"""
用途：将奇林平台用例在研发管理平台上类型设置为自动化
备注：Authorization和Cookie过期时间一天，根据实际情况修改
脚本会自动获取奇林平台已经实现的自动化用例名称，并更新到研发管理平台中测试用例的类型
"""
import json
import requests
from kylintop_operate import get_kylin_testcases
from global_parameter import Authorization, Cookie


def updata_testcases(update_data):
    """
    研发管理平台更新单个用例
    :param update_data:
    :return:
    """
    headers = {"Accept": "application/json, text/plain, */*",
               "Accept-Encoding": "gzip, deflate",
               "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
               "Authorization": Authorization,
               "Connection": "keep-alive",
               "Content-Length": "73",
               "Content-Type": "application/json;charset=UTF-8",
               "Cookie": Cookie,
               "Host": "yf.oetsky.com",
               "Origin": "http://yf.oetsky.com",
               "Referer": "http://yf.oetsky.com/test/repo/1806580592308871169/case",
               "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0", }

    # update_data = json.dumps({"name": "case_type", "id": "1810218827422679041", "value": "自动化测试"})
    res = requests.put("http://yf.oetsky.com/prod-api/dat-test/test/case/updateSingleField", headers=headers,
                       data=update_data)
    print(res)
    print(res.content)
    print(res.text)
    assert res.status_code == 200


def get_yf_testcases_cvt_b():
    """
    获取研发管理平台用例对应关系
    :return:
    """
    headers = {"Accept": "application/json, text/plain, */*",
               "Accept-Encoding": "gzip, deflate",
               "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
               "Authorization": Authorization,
               "Connection": "keep-alive",
               "Content-Length": "73",
               "Content-Type": "application/json;charset=UTF-8",
               "Cookie": Cookie,
               "Host": "yf.oetsky.com",
               "Origin": "http://yf.oetsky.com",
               "Referer": "http://yf.oetsky.com/test/repo/1806580592308871169/case",
               "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0", }

    update_data = json.dumps({"searchType": "list", "size": 100, "current": 1, "testdbId": "1806580592308871169",
                              "conditions": [{"name": "case_catalog_id", "operator": "IN", "type": 3,
                                              "values": ["1806597298046754823"], "ext": {"includeSon": True}}, {}],
                              "categoryIdentifier": "Task",
                              "sortBy": {"name": "create_time", "order": "desc", "type": "8"}})
    res = requests.post("http://yf.oetsky.com/prod-api/dat-test/test/case/pageList", headers=headers,
                        data=update_data)
    # print(res)
    # print(res.text)
    assert res.status_code == 200
    page = res.json().get("data").get("pages")
    print(page)
    list_case = []

    for i in range(page):
        update_data = json.dumps(
            {"searchType": "list", "size": 100, "current": i + 1, "testdbId": "1806580592308871169",
             "conditions": [{"name": "case_catalog_id", "operator": "IN", "type": 3,
                             "values": ["1806597298046754823"], "ext": {"includeSon": True}}, {}],
             "categoryIdentifier": "Task",
             "sortBy": {"name": "create_time", "order": "desc", "type": "8"}})
        res = requests.post("http://yf.oetsky.com/prod-api/dat-test/test/case/pageList", headers=headers,
                            data=update_data)
        # print(res.content)
        list_testcases = res.json().get("data").get("records")
        for testcase in list_testcases:
            list_case.append(
                {"id": testcase.get("id"), "subject": testcase.get("subject"), "code": testcase.get("code")})

    # print(list_case)
    print(len(list_case))
    return list_case


def get_yf_testcases_cvt_a():
    """
    获取研发管理平台用例对应关系
    :return:
    """
    headers = {"Accept": "application/json, text/plain, */*",
               "Accept-Encoding": "gzip, deflate",
               "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
               "Authorization": Authorization,
               "Connection": "keep-alive",
               "Content-Length": "73",
               "Content-Type": "application/json;charset=UTF-8",
               "Cookie": Cookie,
               "Host": "yf.oetsky.com",
               "Origin": "http://yf.oetsky.com",
               "Referer": "http://yf.oetsky.com/test/repo/1806580330911457282/case",
               "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0", }

    update_data = json.dumps({"searchType": "list", "size": 100, "current": 1, "testdbId": "1806580330911457282",
                              "conditions": [{"name": "case_catalog_id", "operator": "IN", "type": 3,
                                              "values": ["1818632222853836803"], "ext": {"includeSon": True}}, {}],
                              "categoryIdentifier": "Task",
                              "sortBy": {"name": "create_time", "order": "desc", "type": "8"}})
    res = requests.post("http://yf.oetsky.com/prod-api/dat-test/test/case/pageList", headers=headers,
                        data=update_data)
    # print(res)
    # print(res.text)
    assert res.status_code == 200
    page = res.json().get("data").get("pages")
    print(page)
    list_case = []

    for i in range(page):
        update_data = json.dumps(
            {"searchType": "list", "size": 100, "current": i + 1, "testdbId": "1806580330911457282",
             "conditions": [{"name": "case_catalog_id", "operator": "IN", "type": 3,
                             "values": ["1818632222853836803"], "ext": {"includeSon": True}}, {}],
             "categoryIdentifier": "Task",
             "sortBy": {"name": "create_time", "order": "desc", "type": "8"}})
        res = requests.post("http://yf.oetsky.com/prod-api/dat-test/test/case/pageList", headers=headers,
                            data=update_data)
        # print(res.content)
        list_testcases = res.json().get("data").get("records")
        for testcase in list_testcases:
            list_case.append(
                {"id": testcase.get("id"), "subject": testcase.get("subject"), "code": testcase.get("code")})

    # print(list_case)
    print(len(list_case))
    return list_case


def updata_cvt_b_case_type():
    """
    更新cvt-b用例类型
    :return:
    """
    filter_list, _ = get_kylin_testcases("cvt-b")
    kylin_case = []
    for case in filter_list:
        case_name = case.get("name").split("_")[0]
        # print(case_name)
        kylin_case.append(case_name)
    # print(kylin_case)
    print(len(kylin_case))
    list_case = get_yf_testcases_cvt_b()
    # print(list_case)

    # 过滤需要修改的研发管理平台用例
    list_setting = []
    for yf_case in list_case:
        # print(yf_case.get("code"))
        if yf_case.get("code") in kylin_case:
            list_setting.append(yf_case)
    print(list_setting)
    print(len(list_setting))

    for setting in list_setting:
        update_data = json.dumps({"name": "case_type", "id": setting.get("id"), "value": "自动化测试"})
        print(update_data)
        updata_testcases(update_data)


def updata_cvt_a_case_type():
    """
    更新cvt-a用例类型
    :return:
    """
    filter_list, _ = get_kylin_testcases("cvt-a")
    kylin_case = []
    for case in filter_list:
        case_name = case.get("name").split("_")[0]
        # print(case_name)
        kylin_case.append(case_name)
    # print(kylin_case)
    print(len(kylin_case))
    list_case = get_yf_testcases_cvt_a()
    # print(list_case)

    # 过滤需要修改的研发管理平台用例
    list_setting = []
    for yf_case in list_case:
        # print(yf_case.get("code"))
        if yf_case.get("code") in kylin_case:
            list_setting.append(yf_case)
    # print(list_setting)
    print(len(list_setting))

    for setting in list_setting:
        update_data = json.dumps({"name": "case_type", "id": setting.get("id"), "value": "自动化测试"})
        print(update_data)
        updata_testcases(update_data)


if __name__ == '__main__':
    # updata_testcases()
    # get_yf_testcases_cvt_b()
    # updata_cvt_b_case_type()

    # get_yf_testcases_cvt_a()
    updata_cvt_a_case_type()
